<?php
namespace app\home\controller;
use think\Db;
use think\captcha\Captcha;
use think\Loader;
class Test extends \think\Controller{
    public function index(){
        return $this->fetch('test');
    }
    public function wx(){
        $appid="wx2349ac57926cd580";
        $appserect="e28531e0fb6784ea37d4590882fc90c9";
        $token=cache('access_token');

        if($token==false){
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appserect}";

            $token=get($url);
            cache('access_token',$token,7200);
        }
        $token=json_decode($token,true);

        $url1="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token['access_token']}";

        $text=[
            "touser"=>"o_8541AIsZi6HqH2uywHWn3fA9rw",
            "msgtype"=>"text",
            "text"=>[
                "content"=>"Hello World",
            ]
        ];
       
        $text=json_encode($text);
        $ret=post($url1,$text);
        dump($ret);
    }

    public function upfile(){
        if(isset($_POST['up'])){
            $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->token['access_token']}&type=image";
            dump($this->token['access_token']);
            $file=request()->file('image');
                if($file){
                    $info = $file->validate(['size'=>4*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
                    if($info){
                        $path=$info->getSaveName();
                        $img_src=ROOT_PATH.'/uploads/'.$path;

                        $data=array(
                            "media"=>new \CURLFile($img_src),
                        );
                        $ret=post($url,$data);
                        dump($ret);
                    }
                }else{
                    $this->error($file->getError());
                }
        }
    }
    
}